//
//  ADScrollView.m
//  CarFace
//
//  Created by dlios on 15/3/23.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "ADScrollView.h"

@interface ADScrollView () <UIScrollViewDelegate>
@property(nonatomic, assign)int flag;// 记录翻到第几页
@property(nonatomic, retain)UIView *view;
@property(nonatomic, retain)UIPageControl *pageControl;
@property(nonatomic, retain)UIScrollView *scroll;
@end

@implementation ADScrollView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.view = [[UIView alloc] initWithFrame:frame];
        self.view.backgroundColor = [UIColor lightGrayColor];
        [self addSubview:self.view];
        [_view release];
        
        self.scroll = [[UIScrollView alloc] init];
        [self.view addSubview:self.scroll];
//        self.scroll.backgroundColor = [UIColor yellowColor];
        self.scroll.showsHorizontalScrollIndicator = NO;
        self.scroll.delegate = self;
        self.scroll.pagingEnabled = YES;
        [_scroll release];
        
        
        self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(frame.size.width / 4, frame.size.height * 9 / 10, frame.size.width / 2, frame.size.height / 10 )];
//        [_pageControl addTarget:self action:@selector(pageAction:) forControlEvents:UIControlEventValueChanged];
        [self.view addSubview:self.pageControl];
        //颜色
        self.pageControl.pageIndicatorTintColor = [UIColor lightGrayColor];
        //关闭交互
//        self.pageControl.userInteractionEnabled = NO;
        self.pageControl.currentPageIndicatorTintColor = [UIColor redColor];
        [_pageControl release];
        
        self.images = [NSMutableArray array];
    }
    
    [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(runPhotoes:) userInfo:nil repeats:YES];

    return self;
    
}

// 传入数组
- (void)setImages:(NSMutableArray *)images
{
    if (_images != images) {
        [_images release];
        _images = [images retain];
    }
//    NSLog(@"%@", self.images);
    
    // 判断数组是否为空
    if (self.images.count == 0) {
        return;
    }
    
    // 处理image数据
    id imageFirst = [self.images firstObject];
    id imageLast = [self.images lastObject];
    [self.images insertObject:imageLast atIndex:0];
    [self.images addObject:imageFirst];
    
    //对scrollViewcontentSize重新赋值
    self.scroll.frame = self.view.bounds;
    self.scroll.contentSize = CGSizeMake(self.frame.size.width * self.images.count , self.frame.size.height);
    self.pageControl.numberOfPages = [self.images count] - 2;
    

    // 每次从scrollview上移除所有子视图
    [self.scroll.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    //解析传入的数组, 进行图片显示, 图片
    for (NSInteger i = 0; i < self.images.count; i++) {
        //UIImage
         UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(i * self.view.frame.size.width, 0, self.view.frame.size.width, self.view.frame.size.height)];
        
        NSString *str = [self.images[i] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        NSURL *url = [NSURL URLWithString:str];
        [imageView setImageWithURL:url];
//        imageView.backgroundColor = [UIColor greenColor];
        [self.scroll addSubview:imageView];
        [imageView release];
    }
    
}

- (void)runPhotoes:(NSTimer *)time
{
//    NSLog(@"%ld", self.images.count - 2);
    [_scroll setContentOffset:CGPointMake((_pageControl.currentPage + 2) * self.view.frame.size.width, 0) animated:YES];
    if (_pageControl.currentPage == self.images.count - 3) {
        _pageControl.currentPage = 0;
        [_scroll setContentOffset:CGPointMake(_pageControl.currentPage * 0, 0)];
    }
}

// pagecontrol
//- (void)changePage:(UIPageControl *)page
//{
//    [_scroll setContentOffset:CGPointMake((page.currentPage + 1) * self.view.bounds.size.width, 0) animated:YES];
//}
//- (void)pageAction:(UIPageControl *)page
//{
//    [_scroll setContentOffset:CGPointMake(self.view.bounds.size.width * (page.currentPage + 1), 0) animated:YES];
//}


//使页面上点的变化与视图的变化一致
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
        int halfX = scrollView.frame.size.width / 2;
    _pageControl.currentPage = (scrollView.contentOffset.x - self.scroll.bounds.size.width - halfX) / (scrollView.frame.size.width) + 1;
}

// 页面跳转
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (self.images.count != 0) {
    if (_scroll.contentOffset.x >= _scroll.bounds.size.width * (self.images.count - 1)) {
        [_scroll setContentOffset:CGPointMake(self.scroll.bounds.size.width, 0) animated:NO];
    }
    if (_scroll.contentOffset.x
         <= 0) {
        [_scroll setContentOffset:CGPointMake(self.scroll.bounds.size.width * (self.images.count - 2), 0) animated:NO];
        }
    }else{
        NSLog(@"错误信息: 轮播区图片数量为0, 实际:%ld", self.images.count);
    }

}

- (void)dealloc
{
    [_view release];
    [_scroll release];
    [_images release];
    [super dealloc];
}
@end
